package org.millioncall.yueyoga.admin.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.yueyoga.admin.dao.GroupDAO;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GroupServiceImpl implements GroupService {
	@Autowired
	private GroupDAO groupDAO;

	@Override
	public List<Group> findGroupsByGroupName(String[] groupNames) {
		if (groupNames.length > 0) {
			return groupDAO.findGroupsByNames(Arrays.asList(groupNames), true);
		}
		return null;
	}

	@Override
	public List<Group> findAllGroups() {
		return groupDAO.findAllGroups();
	}

	@Override
	public List<Group> findAllGroup() {
		return groupDAO.findAllGroup();
	}

	public GroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public PaginatedList<Group> getGroupsInPage(int start, int limit,
			Map<String, Object> map) {
		return groupDAO.findGroupsInPage(start, limit, map);
	}

	@Override
	public boolean saveGroup(Map<String, Object> map) {
		int i = groupDAO.saveGroup(map);
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean saveGroupRole(Map<String, Object> map) {
		int i = groupDAO.saveGroupRole(map);
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean savegrantRoles(String groupId, String roleIds) {
		if (StringUtils.isNotBlank(groupId) && StringUtils.isNotBlank(roleIds)) {
			String[] rIds = roleIds.split(",");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("groupId", groupId);
			int count = 0;
			// --删除用户所在的角色，重新指定角色
			groupDAO.deleteRoleById(groupId);
			for (String roleId : rIds) {
				if (roleId.equals("")) {
					continue;
				} else {
					map.put("roleId", roleId);
					if (groupDAO.savegrantRoles(map) > 0) {
						count += 1;
					}
					map.remove("roleId");
				}
			}
			return count > 0;
		}
		return false;
	}

	@Override
	public boolean saveGrantHall(String groupId, String halls) {
		if (StringUtils.isNotBlank(groupId) && StringUtils.isNotBlank(halls)) {
			String[] hIds = halls.split(",");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("groupId", groupId);
			int count = 0;
			// --删除用户所在的会馆，重新指定会馆
			if (groupDAO.deleteHallById(groupId) >= 0) {
				for (String hId : hIds) {
					if (hId.equals("")) {
						continue;
					} else {
						map.put("hallId", hId);
						if (groupDAO.savegrantHall(map) > 0) {
							count += 1;
						}
						map.remove("hallId");
					}
				}
			}
			return count > 0;
		}
		return false;
	}

	@Override
	public String findEnNameById(String[] strIds) {
		String enName = "";
		if (strIds != null && strIds.length != 0) {
			for (String strId : strIds) {
				if (strId.equals("")) {
					continue;
				} else {
					int id = Integer.parseInt(strId);
					Group group = groupDAO.findGroupById(id);
					enName = enName + group.getName() + ",";
				}
			}
		}
		return enName.substring(0, enName.length() - 1);
	}

	@Override
	public boolean saveGrantGroups(String userId, String[] gpIds) {
		if (StringUtils.isNotBlank(userId) && gpIds != null
				&& gpIds.length != 0) {
			// String[] gpIds = groupsIds.split(",");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			int count = 0;
			// --删除用户所在的用户组，重新指定用户组
			if (groupDAO.deleteGroupUserByUserId(userId) >= 0) {
				for (String groupId : gpIds) {
					if (groupId.equals("")) {
						continue;
					} else {
						map.put("groupId", groupId);
						if (groupDAO.saveGrantGroups(map) > 0) {
							count += 1;
						}
						map.remove("groupId");
					}
				}
			}
			return count > 0;
		}
		return false;
	}

	public boolean updateGroup(Map<String, Object> map) {
		int i = groupDAO.updateGroup(map);
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean updateGroupRole(Map<String, Object> map) {
		int i = groupDAO.updateGroupRole(map);
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Group findGroupById(int id) {
		return groupDAO.findGroupById(id);
	}

	public Group findGroupIdByName(String name) {
		return groupDAO.findGroupByName(name);
	}

	public Group findGroupByChName(String name) {
		return groupDAO.findGroupByChName(name);
	}
	
	@Override
	public void deleteGroupById(int[] id) {
		if (id == null || id.length == 0) {
			throw new RuntimeException(" 未提交要删除组");
		} else {
			this.groupDAO.deleteGroupById(id);
		}
	}

	@Override
	public void deleteGroupRoleById(int[] id) {
		if (id == null || id.length == 0) {
			throw new RuntimeException(" 未提交要删除组");
		} else {
			this.groupDAO.deleteGroupRoleById(id);
		}
	}

	@Override
	public void deleteGroupUserById(int[] id) {
		if (id == null || id.length == 0) {
			throw new RuntimeException(" 未提交要删除组");
		} else {
			this.groupDAO.deleteGroupUserById(id);
		}
	}

	@Override
	public List<Group> findByUserId(String userId) {
		// TODO Auto-generated method stub
		return groupDAO.findByUserId(userId);
	}

}
